package com.gzy.studentsociety.web;

import com.gzy.studentsociety.dao.SocietyDao;
import com.gzy.studentsociety.dao.SocietyDaoImpl;
import com.gzy.studentsociety.dao.StudentDao;
import com.gzy.studentsociety.dao.StudentDaoImpl;
import com.gzy.studentsociety.entity.Society;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@WebServlet(name = "SocietyServlet", value = "/society/*")
public class SocietyServlet extends RouteServlet {
    private SocietyDao societyDao = new SocietyDaoImpl();
    private StudentDao studentDao = new StudentDaoImpl();



    private HttpServletResponse response;
    private HttpServletRequest request;


    @Override
    protected java.lang.Class getMyClass() {
        return this.getClass();
    }
    public String mainIndex(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //初始化社团列表
        List<Society> list = null;
        //获取社团信息
        try {
            list = societyDao.getAllSociety();
        }catch (SQLException throwables) {
            throwables.printStackTrace();
            handleException(throwables,request,response);
        }
        //给社团列表添加创建者信息
        list = list.stream().map(i->{
            try {
                i.setCreator(studentDao.findById(i.getSociety_creator()));
            }catch (SQLException e){
                request.setAttribute("exception",e);
            }
            return  i;
        }).collect(Collectors.toList());
        //list添加到request中
        request.setAttribute("societies",list);
        //转发到mainIndex.jsp
        return "mainIndex";
    }

    public String addSociety(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        String intro = request.getParameter("intro");
        Society society = new Society();
        society.setSociety_intro(intro);
        society.setSociety_name(name);
        society.setSociety_status(1);

        //获取session中登录者为创建者
        String number = (String) request.getSession().getAttribute("student");

        StudentDao studentDao = new StudentDaoImpl();
        SocietyDao societyDao = new SocietyDaoImpl();

        try {
            society.setSociety_creator(studentDao.findByNumber(number).getStudentId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            handleException(throwables, request, response);
        }

        society.setSociety_create_date(new Date());

        int result = -1;
        try {
            result = societyDao.insertSociety(society);
        }catch(SQLException throwables) {
            throwables.printStackTrace();
            handleException(throwables, request, response);
        }

        response.sendRedirect("/studentSociety/society/mainIndex");
        return  "direct";
    }

    public Object updateSociety(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{
        String idStr = req.getParameter("id");
        String name = req.getParameter("name");
        String intro = req.getParameter("intro");
        Integer id = idStr != null ? Integer.valueOf(idStr):null;
        Society society = new Society();
        society.setSocietyId(id);
        society.setSociety_name(name);
        society.setSociety_intro(intro);
        return null;
    }

    public Object changeStatus(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{
        String idStr = req.getParameter("id");
        String statusStr= req.getParameter("status");
        Integer id = Integer.valueOf(idStr);
        Integer status = Integer.parseInt(statusStr);
        Society society = new Society();
        society.setSocietyId(id);
        society.setSociety_status(status);
        int result = -1;
        try {
            result = societyDao.updateSociety(society);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            req.setAttribute("exception",throwables);
            return "error";
        }
        if(result == 1){
            return result;
        }else{
            HashMap<String,String> map = new HashMap<String,String>();
            map.put("message","错误修改");
            return map;
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
